<?php

namespace app\api\controller\product;
use app\api\service\points\PointsDeductService;
use app\common\model\plus\point\PointProduct as PointProductModel;
use app\api\controller\Controller;

/**
 * 积分商品控制器
 */
class PointProduct extends Controller
{
    /**
     * 商品列表
     */
    public function lists()
    {
        $user = $this->getUser();
        $point_ratio = PointsDeductService::getPointRatio($user);
        $page_size = input('page_size', 10);
        // 获取列表数据
        $lists = PointProductModel::with([
            'product' => function ($query) {
                $query->with(['image.file']);
            }
        ])->order(['point_price' => 'asc'])->paginate($page_size)->each(function ($item) use ($point_ratio) {
            if ($point_ratio > 0) {
                $points = ceil($item['point_price'] / $point_ratio);
            } else {
                $points = 0;
            }
            $item->points = $points;
        });
        return $this->renderSuccess('', $lists);
    }
}